package cn.ys.org.controller;

import cn.ys.base.util.AjaxResult;
import cn.ys.base.util.PageList;
import cn.ys.org.domain.SystemDictionary;
import cn.ys.org.query.SystemDictionaryQuery;
import cn.ys.org.service.SystemDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  // @Controller + @ResponseBody
@RequestMapping("/sd")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class SystemDictionaryController {

    @Autowired
    private SystemDictionaryService systemDictionaryService;

//    /**
//     * 之前定义接口的玩法
//     * @return
//     */
//    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
//    public List<SystemDictionary> findAll(){
//        return systemDictionaryService.findAll();
//    }

    /**
     * restful、 风格
     */
    @GetMapping
    public List<SystemDictionary> findAll(){
        return systemDictionaryService.findAll();
    }

//    /**
//     * restful 风格 路径变量
//     *  如果前端传的参数名称 叫 deptId
//      http://localhost:8080/findById? id = 1
//     */
//    @RequestMapping("/findById")
//    public SystemDictionary findById(@RequestParam(value = "deptId",required = true) Long id){
//        return systemDictionaryService.findById(id);
//    }

    /**
     * restful 风格 路径变量
     *  如果前端传的参数名称 叫 deptId
     *  http://localhost:8080/dept/1
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDictionary findById(@ApiParam(value="ID属性",required = true)  @PathVariable("id") Long id){
        return systemDictionaryService.findById(id);
    }

    /**
     * 添加/ 修改
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionary systemDictionary){
        try {
            if (null == systemDictionary.getId()){
                // 添加
                systemDictionaryService.add(systemDictionary);
            }else {
                // 编辑
                systemDictionaryService.update(systemDictionary);
            }
        } catch (Exception e) {
//            throw new RuntimeException(e);
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        systemDictionaryService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页 json格式交互 字符串
     * @RequestBody 接收前端的json数据 转成我们的实体
     *
     */
    @PostMapping
    public PageList<SystemDictionary> queryByPage(@RequestBody SystemDictionaryQuery quey){
        return systemDictionaryService.queryByPage(quey);
    }

    // 批量删除
    @PatchMapping
    public AjaxResult piDeleteById(@RequestBody List<Long> ids){
        systemDictionaryService.piDeleteById(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/tree")
    public List<SystemDictionary> deptTree() {
        return systemDictionaryService.deptTree();
    }

}
